<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :checkedit="checkedit" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {subOnLoad, changeEditor,configSelectData} from '../../../static/js/formUtil.js';
	import { ignoreHtml,ptFloat,minusF,sumF, getDateStr, errorModal} from '@/static/js/common.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return {
				initValue:{
					posId: "",
					instId: "",
					fileList:[]//上次保存的文件列表
				},
				autoformdata: [],
				cachename:'',
				index:null,
				datas:[],
				order:0,
				formdata:{},
				instCacheKey:'fin-bx-expenses-data-info-inst-cache',
				userCacheKey:'fin-bx-expenses-data-info-user-cache',
				checkedit:false,
				formSourceCache:'fin-bx-expenses-data-info-formsource-cache',
				expenseCategoryCacheKey:'fin-bx-expenses-title2-expenseCategory-cache',
				expenseTypeCache:'fin-bx-expenses-title2-expenseType-cache'
			}
		},
		watch:{
			formdata:{
				handler(newValue,oldValue){
					let _that = this.$refs.autoform
					_that.formdata.sumCosts = sumF(
						[
							this.formdata.longCosts,
							this.formdata.longCosts1,
							this.formdata.shortCosts,
							this.formdata.stayCosts,
							this.formdata.eatCosts,
							this.formdata.subsidyCosts,
							this.formdata.tollCosts,
							this.formdata.oilCosts,
							this.formdata.otherCosts
						]
					)
				},
				deep:true
			},
		},
		methods: {
			clearExpenseCategory(){
				let _that = this
				_that.$refs.autoform.clear("title2","expenseCategoryId")
				_that.$refs.autoform.clear("title2","expenseTypeId")
			},
			verifyExpenseType(){
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				if(formdata.expenseCategoryId){
					_that.$refs.autoform.changeEditor("title2","expenseTypeId",{
						link:'../../public/search/search',
						"linkParam":{
							other:JSON.stringify({
								id: formdata.expenseCategoryId
							}),
							url:'expenseCategory/getExpenseByParent',
							namefield:'typeName',
							initload:true,
							cachename:_that.expenseTypeCache
						}
					})
					return true
				}else{
					let title = _that.$refs.autoform.getEditorTitle("title2","expenseCategoryId")
					_that.om.toast(title+'需优先完成编辑！')
					return false
				}
			},
			cleearUser(){
				let _that = this
				_that.$refs.autoform.clear("title2","userId")
				_that.$refs.autoform.clear("title2","posId")
				_that.$refs.autoform.clear("title2","instId")
			},
			clearBusinessTravel(){
				let formdata = this.$refs.autoform.formdata
				formdata.businessTravelId = "";
				formdata.businessTravelDataId = "";
				formdata.businessTravelCode = "";
				formdata.planStartTime = "";
				formdata.planEndTime = "";
				formdata.startTime = "";
				formdata.endTime = "";
				formdata.cityName = "";
				formdata.address = "";
				formdata.vehicleTypeId = "";
				formdata.vehicleTypeName = "";
			},
			onSelectAccount(account){
				this.$refs.autoform.formdata.accountCode = account.code;
				if(account.layerNum == 1) {
					this.$refs.autoform.formdata.accountParentId = account.id;
					this.$refs.autoform.formdata.accountParentName = account.dicname;
				}else{
					let _that = this
					this.om.request("accountDict/findLevel1Account",{
						accountDictId: account.id
					}).then(res=>{
						if(res.succeed){
							var value = res.value
							_that.$refs.autoform.formdata.accountParentId = value.id;
							_that.$refs.autoform.formdata.accountParentName = value.dicname;
						}else{
							_that.om.toast(res.msg)
						}
					})
				}
			},
			clearAccount(){
				var formdata = this.$refs.autoform.formdata
				this.$refs.autoform.clear("title2", "accountId")			
				formdata.accountCode = ""
				formdata.accountParentId = ""
				formdata.accountParentName = ""
			},
			verifyStartTime(datetime){
				var currentTime = getDateStr(new Date())
				var endTime = this.formdata.endTime || currentTime
				
				if(datetime > endTime){
					return "开始时间不大于当前时间和结束时间"
				}else{
					return true
				}
				
			},
			verifyEndTime(datetime){
				var currentTime = getDateStr(new Date())
				var startime = this.formdata.startTime
				
				if(startime){
					if(datetime < startime){
						return "结束时间不小于开始时间"
					}else if(datetime > currentTime){
						return "结束时间不大于当前时间"
					}else{
						return true
					}
				}else if(datetime > currentTime){
					return "结束时间不大于当前时间"
				}else{
					return true
				}
			},
			verifyFormSource(){
				let _that = this
				if(!_that.formdata.formSourceTypeId){
					let title = _that.$refs.autoform.getEditorTitle("title2","formSourceTypeId")
					return "请选择"+title;
				}
				return true
			},
			onSelectFormSourceType(item){
				let _that = this
				_that.$refs.autoform.changeEditor("title2", "formSourceId", {
					link:"/pages/public/pageSearch/pageSearch",
					linkParam:{
						other:JSON.stringify({
							formSourceTypeId:item.id,
							oldValue:_that.formdata.formSourceId
						}),
						initload:true,
						namefield:"code|zuserName",
						notefield:"objCode|objName",
						extrafield:"ztime",
						searchfield:"q",
						url:"payApplicationForm/findFormPage",
						cachename:_that.formSourceCache,
						placeholder:"请输入制表人、表单编码、合同编码"
					}
				})
			},
			clearFormSourceType(){
				let _that = this
				_that.$refs.autoform.clear("title2", "formSourceTypeId")
				_that.$refs.autoform.clear("title2", "formSourceId")
			},
			initLoad(){
				let _that = this
				var pro1 = _that.om.request('accountDict/getCombobox', {selectInBudget: true});
				//加载付款类别（预付、应付、其他）
				var pro2 = _that.om.request('dictZero/getListByCode', { code: "37851" });
				//加载预算识别（非预算、预算内）
				var pro3 = _that.om.request('dictZero/getListByCode', { code: "37861" });
				//来源单据
				var pro4 = _that.om.request('payApplicationForm/findFormTypeList');
				Promise.all([pro1, pro2, pro3, pro4]).then(res => {
					_that.$refs.autoform.changeEditor("title2", "accountId", "data", configSelectData(res[0],"id","dicname"))
					_that.$refs.autoform.initSelectIndex("title2", "accountId")
					
					_that.$refs.autoform.changeEditor("title2", "payCategoryId", "data", configSelectData(res[1],"id","dicname"))
					_that.$refs.autoform.initSelectIndex("title2", "payCategoryId")
					
					_that.$refs.autoform.changeEditor("title2", "budgetTypeId", "data", configSelectData(res[2],"id","dicname"))
					if(!_that.$refs.autoform.formdata.budgetTypeId){
						_that.$refs.autoform.initSelectVal("title2", "budgetTypeId")
					}else{
						_that.$refs.autoform.initSelectIndex("title2", "budgetTypeId")
					}
					_that.$refs.autoform.changeEditor("title2", "formSourceTypeId", "data", configSelectData(res[3],"id","businessfun"))
					_that.$refs.autoform.initSelectIndex("title2", "formSourceTypeId")
				})
			}
		},
		onLoad(e){
			let _that = this
			subOnLoad(_that, e, function() {
				_that.initValue.userId = e.userId
				_that.initValue.userName = e.userName
				_that.initValue.posId = e.posId
				_that.initValue.posName = e.posName
				_that.initValue.instId = e.instId
				_that.initValue.instName = e.instName
				if(e.configDataCacheKey){
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							//if(res.data && res.data instanceof Array){
							if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								_that.autoformdata = res.data
								//if(_that.formdata.fileList instanceof Array){
								if(Object.prototype.toString.call(_that.formdata.fileList)==='[object Array]'){
									Object.assign(_that.initValue, _that.formdata)
								}else{
									_that.formdata.fileList = new Array
									Object.assign(_that.initValue, _that.formdata)
								}
								_that.autoformdata[0].order = _that.order
								changeEditor(_that.autoformdata[0].editors, "userId", "link", "../../public/pageSearch/pageSearch")
								changeEditor(_that.autoformdata[0].editors, "userId", "linkParam", {
									other: JSON.stringify({
										firstUserId: e.userId
									}),
									namefield:"userPosInstName",
									searchfield:"q",
									initload:true,
									url:"orgInstPosUserOfficial/findPagerByOrgId",
									cachename:_that.userCacheKey,
									placeholder:"请输入姓名或岗位"
								})
								changeEditor(_that.autoformdata[0].editors, "partakeInstId","link","../../public/search/search")
								changeEditor(_that.autoformdata[0].editors, "partakeInstId", "linkParam", {
									method:"post",
									namefield:"instname",
									searchfield:"instName",
									initload:true,
									url:"tinstitution/findNotBoard1",
									cachename:_that.instCacheKey,
									placeholder:"请输入机构名称"
								})
								changeEditor(_that.autoformdata[0].editors, "expenseCategoryId","link","../../public/pageSearch/pageSearch")
								changeEditor(_that.autoformdata[0].editors, "expenseCategoryId", "linkParam", {
									namefield:"typeName",
									searchfield:"q",
									initload:true,
									url:"expenseCategory/getExpenseCategoryPage",
									cachename:_that.expenseCategoryCacheKey,
								})
								if(_that.formdata.formSourceTypeId){
									changeEditor(_that.autoformdata[0].editors, "formSourceId", {
										link:"/pages/public/pageSearch/pageSearch",
										linkParam:{
											other:JSON.stringify({
												formSourceTypeId:_that.formdata.formSourceTypeId,
												oldValue:_that.formdata.formSourceId
											}),
											initload:true,
											namefield:"code|zuserName",
											notefield:"objCode|objName",
											extrafield:"ztime",
											searchfield:"q",
											url:"payApplicationForm/findFormPage",
											cachename:_that.formSourceCache,
											placeholder:"请输入制表人、表单编码、合同编码"
										}
									})
								}
								changeEditor(_that.autoformdata[0].editors, "fileList", "fileUploadKey", "5")
								if(_that.formdata.fileList) {
									var tempFileList = []
									var fileUrlList = _that.formdata.fileList.map(t=>{
										tempFileList.push(t.fileSaveName)
										return t.fileSaveName
									})
									
									changeEditor(_that.autoformdata[0].editors, "fileList", "fileUrlList", fileUrlList)
									changeEditor(_that.autoformdata[0].editors, "fileList", "tempFileList", tempFileList)
								}
								_that.initLoad()
							}
						}
					});
				}
			})
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key: _that.userCacheKey,
					success: function(res) {
						if(res.data){
							let d = res.data
							var formdata  = _that.$refs.autoform.formdata
							formdata.userId = d.userId
							formdata.userName = d.userName
							formdata.posId = d.posId
							formdata.posName = d.posName
							formdata.instId = d.instId
							formdata.instName = d.instName
							formdata.partakeInstId = d.instId
							formdata.partakeInstName = d.instName
							uni.removeStorage({
								key: _that.userCacheKey
							});
						}
					}
				});
				uni.getStorage({
					key: _that.expenseCategoryCacheKey,
					success: function(res) {
						if(res.data){
							let d = res.data
							var formdata  = _that.$refs.autoform.formdata
							formdata.expenseCategoryId = d.id
							formdata.expenseCategoryName = d.typeName
							_that.$refs.autoform.clear("title2","expenseTypeId")
							uni.removeStorage({
								key: _that.expenseCategoryCacheKey
							});
						}
					}
				});
				uni.getStorage({
					key: _that.instCacheKey,
					success: function(res) {
						if(res.data){
							let d = res.data
							var formdata  = _that.$refs.autoform.formdata
							formdata.partakeInstId = d.id
							formdata.partakeInstName = d.instname
							uni.removeStorage({
								key: _that.instCacheKey
							});
						}
					}
				});
				uni.getStorage({
					key: _that.formSourceCache,
					success: function(res) {
						if(res.data){
							let d = res.data
							var formdata  = _that.$refs.autoform.formdata
							formdata.formSourceId = d.id
							formdata.formSourceNum = d.code
							uni.removeStorage({
								key: _that.formSourceCache
							});
						}
					}
				});
				uni.getStorage({
					key:_that.expenseTypeCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.expenseTypeId = d.id
							formdata.expenseTypeName = d.typeName
							uni.removeStorage({
								key:_that.expenseTypeCache
							})
						}
					}
				})
			}
		}
	}
</script>

<style>

</style>
